<template>
  <div>
    <el-dialog ref="editDialog" v-model="openFlag" title="添加打印模板" width="50%" append-to-body>
      <el-container>
        <el-aside width="200px">
          <h3>字段列表</h3>
          <div class="scrollable-list">
            <div role="group" aria-label="checkbox-group" class="el-checkbox-group">
              <label v-for="field in fields" :key="field.value" class="el-checkbox" :class="{ 'is-checked': field.checked, 'is-disabled': field.disabled }">
              <span class="el-checkbox__input" :class="{ 'is-checked': field.checked, 'is-disabled': field.disabled }">
                <span class="el-checkbox__inner"></span>
                <input type="checkbox" class="el-checkbox__original" :value="field.value" :disabled="field.disabled" v-model="field.checked" @change="handleCheckboxChange(field)">
              </span>
                <span class="el-checkbox__label">{{ field.label }}</span>
              </label>
            </div>
          </div>
        </el-aside>
        <el-main>
          <div class="encoding-position">
            <div class="label">编码位置</div>
            <el-radio-group v-model="form.codePosition" class="ml-4">
              <el-radio label="top" size="large" v-if="form.labelType==='1'">上</el-radio>
              <el-radio label="bottom" size="large" v-if="form.labelType==='1'">下</el-radio>
              <el-radio label="left" size="large" v-if="form.labelType==='2'">左</el-radio>
              <el-radio label="right" size="large" v-if="form.labelType==='2'">右</el-radio>
            </el-radio-group>

          </div>
          <div class="item">
            <div class="label">预览效果</div>
            <tag-display-preview :label-type="form.labelType" :selected-fields="selectedFields" :code-position="form.codePosition"></tag-display-preview>
          </div>

        </el-main>
      </el-container>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>


</template>

<script setup>
import { ref, computed } from 'vue';
import api from './api.js'
import TagDisplayPreview from "@/views/asset/settings/tag/TagDisplayPreview.vue";

const openFlag = ref(false);

const fields = ref([
  { label: '公司名称', value: 'companyName', checked: false, disabled: false },
  { label: '资产名称', value: 'name', checked: false, disabled: false },
  { label: '资产分类', value: 'categoryName', checked: false, disabled: false },
  { label: '品牌', value: 'brand', checked: false, disabled: false },
  { label: '型号', value: 'model', checked: false, disabled: false },
  { label: '购置时间', value: 'buyTime', checked: false, disabled: false },
  { label: '购置金额', value: 'buyPrice', checked: false, disabled: false },
  { label: '管理员', value: 'adminName', checked: false, disabled: false },
  { label: '仓库', value: 'warehouseName', checked: false, disabled: false },
  { label: '使用人', value: 'userName', checked: false, disabled: false },
  { label: '使用部门', value: 'deptName', checked: false, disabled: false },
  { label: '供应商', value: 'supplierName', checked: false, disabled: false },
]);

const selectedRadio = ref('bottom');
const barcodeImage = ref(''); // 省略实际的base64字符串

const previewFields = computed(() => {
  return fields.value.filter(field => field.checked).slice(0, 4).map(field => field.label);
});

const form = reactive({
  codePosition: 'bottom',
  labelType: '1'
});

const close = () => {
  openFlag.value = false; // 返回到上一个页面
};

const emit = defineEmits(['sub']);

const selectedFields = computed(() => {
  return fields.value.filter(field => field.checked);
})

function save() {
  form.fields = selectedFields.value.map(field => field.value).join(',');
  console.log("选中的字段:", selectedFields);
  api.add(form).then(()=> {
    emit('sub');
    close();
  });
}

const selectRadio = (value) => {
  selectedRadio.value = value;
};

const handleCheckboxChange = () => {
  const checkedCount = fields.value.filter(field => field.checked).length;

  if (checkedCount === 0) {
    fields.value[0].checked = true;
  }

  if (checkedCount >= 4) {
    fields.value.forEach(field => {
      if (!field.checked) {
        field.disabled = true;
      }
    });
  } else {
    fields.value.forEach(field => {
      field.disabled = false;
    });
  }
};

const moveBarcode = (direction) => {
  if (direction === 'up' && selectedRadio.value === 'bottom') {
    selectedRadio.value = 'top';
  } else if (direction === 'down' && selectedRadio.value === 'top') {
    selectedRadio.value = 'bottom';
  }
};

function open(params) {
  openFlag.value = true;
  if (params.labelType === '1' || params.labelType == 1) {
    form.codePosition = 'bottom';
  } else {
    form.codePosition = 'left'
  }
  Object.assign(form, params);
}

defineExpose({
  open
})
</script>

<style scoped>

.field-list h3 {
  font-size: 20px;
  margin-bottom: 10px;
}

.scrollable-list {
  height: 200px;
  overflow-y: auto;
}

.scrollable-list label {
  display: block;
  margin-bottom: 5px;
}

.img {
  width: 400px;
}

.encoding-position {
  width: 300px;
  margin-bottom: 20px;
}

.preview {
  border: 1px solid #ddd;
  padding: 10px;
  min-height: 100px;
  display: flex;
}

.button-group {
  margin-top: 10px;
}

.button-group button {
  margin-right: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  background-color: #5cb85c;
  color: white;
  cursor: pointer;
  margin-left: 10px;
}

button:hover {
  background-color: #4cae4c;
}

button:active {
  background-color: #3d8b3d;
}
</style>
